<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
     <SCRIPT type="text/javascript">
         function isEmail(str) {
			if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
				return true;
			} else {
				return false;
		    }
		 }
		
         function saveEmployee(){
              var userName = $("#empluser").val();
              var inviteCode = $("#inviteCode").val();
              var nickName = $("#nickName").val();
              var userId = $("#userId");
              
              var employeeId = '<s:property value="employee.id"/>';
              var typeName = '<s:if test="#request.type == 0">员工</s:if><s:else>访客</s:else>';
              
              var setEmployeeId = '<s:property value="#parameters.setTxtId" />';
              var setEmployeeNickname = '<s:property value="#parameters.setNickname" />';
              var errorMsg = "";
              
              if(nickName.replace(/\s/g , '') =='' || nickName.length <=0){
                errorMsg+= (typeName + '名称不能为空');
              }
              
              
              if(userName!='' && !isEmail(userName)){
            	  errorMsg+='cts用户名必须为您的email地址';
              }
              
              if(errorMsg !=''){
                errorMsg = errorMsg.replace(/,$/ , '');
                alert(errorMsg);
                return;
              }
              
              var url = 'cts/vailEmpNickName';
              if($(userId).val() == ''){
                url += "?nickname="+ nickName + "&findtype=0&id="+employeeId;
              }else{
                var uid = $(userId).val();
                url += "?userid="+ uid + "&findtype=3&nickname="+nickName+"&id="+employeeId;;
              }
              
              var checkName = function(){
                 $.post(url , "" , function(data){
                     if(data == 1){
                        alert('此员工已是公司的成员');                    
                     }else if(data == 2 || data == 3){
                        var workType = (data == 2) ? '员工' : '访客';
                        var userName = $("#empluser").val()
                        alert(userName + '已是公司的' + workType);  
                     }else if(data == 0){
                        $(document).dequeue("saveEmployee");
                     }else{
                        alert("出错了，请稍后再试");
                     }
                 });
              }
              
              var saveEmployee = function(){
                 returnVal();
            	 var param = $("#employeeForm").serialize();
            	 $.post("cts/saveEmployee" , param , function(data){
            	      if(data == -1){
            	         alert("名称已存在");
            	      }else if(data == -2){
            	         alert("邀请用户已是公司成员");
            	      }else if(data > 0){
            	         alert("操作成功");
            	         if(setEmployeeId != '' && $('#'+setEmployeeId).val() == ''){
	            		  	$('#'+setEmployeeId).val(data);
	            		 }
	            		 
	            		 if(setEmployeeNickname != '' && $('#'+setEmployeeNickname).val() == ''){
	            		  	$('#'+setEmployeeNickname).val($('#nickName').val());
	            		 }
	            		 
	            		 var noplay = '<s:property value="#request.noplay"/>'; // 是否重刷页面
	            		 if(noplay != 'true' || noplay == ''){
	            		    window.location.reload();
	            		 }else{
	            		  	hidePopDiv($('#employeeForm').parents('.popdiv_inner:first').attr('id'));
	            		 }
            	      }else{
            	        alert("操作失败");
            	      }
            	 });
              }
              
              $(document).queue("checkName" , checkName);
              $(document).queue("saveEmployee" , saveEmployee);
              $(document).dequeue("checkName");
         }
         
         function returnVal(){
           $("#authList").find("input[type='checkbox']").each(function(){
               if($(this).is(":checked")){
                  $(this).val(1);
               }else{
                 $(this).val(0);
               }
           });
         }
         function searchUser(obj , e){
            if(bindNextPrev(obj , e)){
               return;
            }
            
            var name = $(obj).val();
            if(name.replace(/\s/g , '') == ""){
               $("#userId").val('');
               $("#userlist").hide();
               return;
            }
            $.post("cts/fuzzyUser.action?name="+name , "" , function(data){
                  var data2 = eval("("+data+")");
                  showUserList(data2 , obj);
            });
         }
         
         function showUserList(data , obj){
            if(data.length <= 0){
               $("#userId").val('');
               $("#userlist").hide();
               return;
            }
            var mObj = $("#employeeForm").find("div[id='userlist']");
            if(mObj == null || $(mObj).html() == null ){
              var divObj = '<div id="userlist" class="userlist"><ul style="width:100%;"></ul></div>';
              $(obj).before(divObj);
            }
            
            var li="";
            for(var i=0;i<data.length;i++){
               li += '<li id='+data[i]["userId"]+'>'+data[i]["userName"]+'</li>';
            }
            
            $("#userlist").find("ul").html(li);
            $("#userlist").find("li").each(function(){
                $(this).click(function(){
                    selectUser(this);
                    $("#userlist").hide();
                }).mouseover(function(){
                    $(this).css({"background-color":"#ececec"});
                }).mouseout(function(){
                    $(this).css({"background-color":"#ffffff"});
                });
            });
            
            var top = $(obj).offset().top+10;
            var left = $(obj).offset().left;
            
            top = 25;
            left = 0;
            $("#userlist").css({
               "margin-top":top+"px",
               "margin-left":left+"px"
            }).show();
         }
         
         function selectUser(obj){
            var id = $(obj).attr("id");
            var name = $(obj).text();
            $("#userId").val(id);
            $("#empluser").val(name);
         }
         
         function initWinMsg(){
            var status = '<s:property value="employee.status"/>';
            var userid = '<s:property value="employee.user.id"/>';
            if(status == 0 && userid == ''){
               
            }
          
         }
         
        function myOnblur(obj , hideId){
           var time = null;
           time = window.setTimeout(function(){
               window.clearTimeout(time);
               $("#"+hideId).hide();
           } , 500);
        }
     </SCRIPT>
     
     <script type="text/javascript">
		var selectObject = null;
		function mouseMoveCha(){
		    var contentlist = $("#userlist");
		    contentlist.each(function(i){
		        $(this).mouseout(function(){
		            //selectObject = this;
		    		$(contentlist).find("li").css({"background-color":"#fff"});
		            $(this).css({"background-color":"#ddd"});
		        }).mouseover(function(){
		        
		        });
		    });
			      
		}
		
        function bindNextPrev(obj , e){
            mouseMoveCha();
            
            var theEvent = window.event || e;
	        if(typeof(theEvent) == 'undefined'){
	          return true;
	        }
	        
		    var keyFee = theEvent.keyCode || theEvent.which;
		    var isGo = false;
			var contentlist = $("#userlist");
		    
		    if (keyFee == 38) {// prev
		        if(selectObject == null){
		           selectObject = $(contentlist).find("li:last");
		        }else{
		           selectObject = $(selectObject).prev();
		           if($(selectObject).html() == null){
		              selectObject = $(contentlist).find("li:last");
		           }
		        }
			    isGo = true;
			}
			
			if (keyFee == 40) {// next
			    if(selectObject == null){
		           selectObject = $(contentlist).find("li:first");
		        }else{
		           selectObject = $(selectObject).next();
		           if($(selectObject).html() == null){
		              selectObject = $(contentlist).find("li:first");
		           }
		        }
			    isGo = true;
			}
			
			if(keyFee == 13){ // enter
			    if(selectObject!=null && $(selectObject).html() !=null){
			        selectUser(selectObject);
			    }
			    
			    $(contentlist).hide();
			    return true;
			}
			
			if(keyFee == 27){
			   $(contentlist).hide();
			    return true;
			}
			
			// change the background color
			if(isGo == true){
			   if($(selectObject).html() !=null){
			      $(contentlist).find("li").css({"background-color":"#fff"});
			      $(selectObject).css({"background-color":"#ddd"});
			   }
			   
			   return true;
			}
        }
        
     </script>
     
     <style type="text/css">
       #employeeForm table{
         font-size:12px;
       }
       
       .txtright{
         text-align: right;
       }
       .authDiv{
         height:14px;width:120px;float:left;
       }
       
       .userlist{
         width:200px;
         height:auto;
         border:1px solid gray;
         background-color:white;
         position: absolute;
       }
       
       .userlist ul li{
          width:100%;
          height:20px;
          line-height:20px;
          border-bottom:1px dotted gray;
          font-size:12px;
       }
       
       .background1{
         background-color:#ececec;
       }
       
       .background2{
         background-color:#ffffff;
       }
     </style>
  </head>
  
  <body>
     <s:if test="#request.type == 0 || #request.type == 1">
       <form action="cts/saveEmployee.action" id="employeeForm" autocomplete="off">
            <input type="hidden" id="isUpUser" name="isUpUser" value="<s:property value="#request.isUpUser"/>"/> 
            <input type="hidden" id="toUrl" name="toUrl" value="${pageContext.request.contextPath}/cts "/> 
	        <table cellpadding="0" cellspacing="0" align="center">
	         <tr>
	           <td class="txtright">
	              <s:if test="#request.type == 0">员工</s:if><s:else>访客</s:else>名称：
	           </td>
	           <td class="twidth">
	              <input type="text" id="nickName" name="employee.nickname" value="<s:property value="employee.nickname"/>"/>
	           </td>
	         </tr>
	         <s:if test="#request.type != 0">
		         <tr>
		           <td class="txtright">
		              全称：
		           </td>
		           <td class="twidth">
		              <input type="text" id="fullname" name="employee.fullname" value="<s:property value="employee.fullname"/>"/>
		           </td>
		         </tr>
	         </s:if>
	         <tr>
	           <td style="width:30%" class="txtright"><div style="width:100px;"><s:if test="#request.type == 0">员工</s:if><s:else>访客</s:else>用户(邮箱)：</div></td>
	           <td class="twidth">
	              <s:if test="employee.user!=null || employee.user.id !=0 ">
	                 <span><s:property value="employee.user.userName"/></span>
	                  <input type="hidden" id="empluser" name="userName" value="<s:property value="employee.user.userName"/>"/>
	              </s:if>
	              <s:else>
	                  <input type="text" id="empluser" name="userName" value="<s:property value="employee.user.userName"/>" onkeyup="searchUser(this , event)" onblur="myOnblur(this , 'userlist')"/>
	              </s:else>
		          <input type="hidden" id="type" name="employee.type" value="<s:property value="#request.type"/>" />
	              <input type="hidden" id="employeeId" name="employeeId" value="<s:property value="employee.id"/>"/>
	              <input type="hidden" id="empInvit" name="employee.invite" value="<s:property value="employee.invite"/>"/>
		          <input type="hidden" id="userId" name="userId" value="<s:property value="employee.user.id"/>"/>
	           </td>
	         </tr>
	         <tr style="display:none;">
	           <td class="txtright">邀请码：</td>
	           <td class="twidth">
	             <input readonly="readonly" type="text" id="inviteCode" name="employee.inviteCode" value="<s:property value="employee==null ? #request.code : employee.inviteCode"/>"/>
	           </td>
	         </tr>
	         <s:if test="employee !=null">
	             <tr>
		            <td class="txtright">用户类型：</td>
		            <td class="twidth">
		              <s:if test="#request.type == 0">员工</s:if>
		              <s:else>访客</s:else>
		            </td>
		         </tr>
	         </s:if>
	         <tr>
	            <td class="txtright">用户状态：</td>
	            <td>
	               <input type="radio" id="yeuse" name="employee.status" value="1" <s:property value="employee == null || employee.status==1 || employee.status==0 ? 'checked' :''"/> />正常
	               <input type="radio" id="nouse" name="employee.status" value="2" <s:property value="employee.status!=1 ? 'checked' :''"/> />禁用
	            </td>
	         </tr>
	         <s:if test="#request.type == 0">
		         <tr>
		            <td class="txtright" style="vertical-align: top">权限：</td>
		            <td id="authList">
		              <div class="authDiv">
		                <input type="checkbox" id="creatWork" name="employee.auth1" value="0" <s:property value="employee.auth1==1 ? 'checked' : '' "/>  />创建订单
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatReward" name="employee.auth2" value="0" <s:property value="employee.auth2==1 ? 'checked' : '' "/>  />创建订单奖励
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatItem" name="employee.auth3" value="0" <s:property value="employee.auth3==1 ? 'checked' : '' "/>  />创建作业
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatItem" name="employee.auth10" value="0" <s:property value="employee.auth10==1 ? 'checked' : '' "/>  />创建费用
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatTag" name="employee.auth4" value="0" <s:property value="employee.auth4==1 ? 'checked' : '' "/>  />创建标签
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatMan" name="employee.auth5" value="0" <s:property value="employee.auth5==1 ? 'checked' : '' "/>  />创建访客
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatDoc" name="employee.auth6" value="0" <s:property value="employee.auth6==1 ? 'checked' : '' "/>  />创建文档模版
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatIncome" name="employee.auth7" value="0" <s:property value="employee.auth7==1 ? 'checked' : '' "/>  />创建财务收入
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatSpending" name="employee.auth8" value="0" <s:property value="employee.auth8==1 ? 'checked' : '' "/>  />创建财务支出
		              </div>
		              <div class="authDiv">
		                <input type="checkbox" id="creatMenonyReward" name="employee.auth9" value="0" <s:property value="employee.auth9==1 ? 'checked' : '' "/>  />创建财务奖励
		              </div>
		            </td>
		         </tr>
	         </s:if>
	         <tr>
	          <s:if test="employee!=null">
	              <td colspan="2" align="center" style="padding-top:15px;">
	          </s:if>
	          <s:else>
		           <td colspan="2" align="center" style="padding-top:30px;">
	          </s:else>
	              <input type="button" id="enterSave" name="n_enterSave" value="确认" onclick="saveEmployee()"/>
	              &nbsp;&nbsp;
	              &nbsp;&nbsp;
	              <input type="reset" id="reset" name="n_reset" value="重置"/>
	           </td>
	         </tr>
	      </table>
       </form>
       
       
     </s:if>
  </body>
</html>
